﻿var map;
var infoWin;
var polygon = new Array();
var polygon_new = null;
$(function(){
    treeEvent();
    treeEvent1();
    treeEvent2();
});
function treeEvent()
{
    $(".easyui-tree").tree({
        onClick: function(node){
            var level = node.level;
            var area_id = node.area_id;
            var area_name = node.text;
            if(level==4) {
                $("#area_id").val(area_id);
                $("#area_name").val(area_name);
                /*社区可编辑*/
                var len = polygon.length;
                for(var i=0;i<len;i++) {
                    polygon[i].disableEditing();
                    if(area_id==polygon[i].area_id) {
                        polygon[i].enableEditing();
                    }
                }
                /*新增的社区设置为空*/
                map.removeOverlay(polygon_new);
                polygon_new = null;

            } else {
                $("#area_id").val('');
                $("#area_name").val('');
                /*社区可编辑*/
                var len = polygon.length;
                for(var i=0;i<len;i++) {
                    polygon[i].disableEditing();
                }
                map.removeOverlay(polygon_new);
                polygon_new = null;
            }

        },
        onExpand: function(node){
            var area_id = node.area_id;
            var level = node.level;
            if(level==3) {
                var childrenNodes = $('.easyui-tree').tree('getChildren',node.target);
                var len = childrenNodes.length;
                for(var i=0;i<len;i++) {
                    $('.easyui-tree').tree('remove',childrenNodes[i].target);
                }
                getChildArea(node,area_id);
            }
        }
    });
}
function getChildArea(node,area_id)
{
    $.ajax({
        type: "GET",
        url: "/admin/getChildPoints",
        data: {area_id:area_id},
        success: function (result) {
            var len = result.length;
            var data = [];
            for(var i=0;i<len;i++) {
                var tmp_obj = new Object();
                tmp_obj.text = "<a style='color: #000000'  target='myFrameName' href='chartcontent?level=4&city="+result[i].id+"'>"+result[i].name+"</a>";
                tmp_obj.state = 'open';
                tmp_obj.level = 4;
                tmp_obj.area_id = result[i].id;
                data.push(tmp_obj);
            }
            console.log(data);
            $('.easyui-tree').tree('append', {
                parent: node.target,
                data: data
            });

        },
        error: function () {
            $.messager.alert('系统提示', "数据加载失败！", "error");
        }
    });
}

function treeEvent1()
{
    $(".easyui-tree1").tree({
        onClick: function(node){
            var level = node.level;
            var area_id = node.area_id;
            var area_name = node.text;
            if(level==4) {
                $("#area_id1").val(area_id);
                $("#area_name").val(area_name);
                /*社区可编辑*/
                var len = polygon.length;
                for(var i=0;i<len;i++) {
                    polygon[i].disableEditing();
                    if(area_id==polygon[i].area_id) {
                        polygon[i].enableEditing();
                    }
                }
                /*新增的社区设置为空*/
                map.removeOverlay(polygon_new);
                polygon_new = null;

            } else {
                $("#area_id1").val('');
                $("#area_name").val('');
                /*社区可编辑*/
                var len = polygon.length;
                for(var i=0;i<len;i++) {
                    polygon[i].disableEditing();
                }
                map.removeOverlay(polygon_new);
                polygon_new = null;
            }

        },
        onExpand: function(node){
            var area_id = node.area_id1;
            var level = node.level;
            if(level==3) {
                var childrenNodes = $('.easyui-tree1').tree('getChildren',node.target);
                var len = childrenNodes.length;
                for(var i=0;i<len;i++) {
                    $('.easyui-tree1').tree('remove',childrenNodes[i].target);
                }
                getChildArea1(node,area_id);
            }
        }
    });
}
function getChildArea1(node,area_id)
{
    $.ajax({
        type: "GET",
        url: "/admin/getChildPoints",
        data: {area_id:area_id},
        success: function (result) {
            var len = result.length;
            var data = [];
            for(var i=0;i<len;i++) {
                var tmp_obj = new Object();
                tmp_obj.text = "<a style='color: #000000' target='myFrameName' href='checkchartcontent?level=4&city="+result[i].id+"'>"+result[i].name+"</a>";
                tmp_obj.state = 'open';
                tmp_obj.level = 4;
                tmp_obj.area_id = result[i].id;
                data.push(tmp_obj);
            }
            console.log(data);
            $('.easyui-tree1').tree('append', {
                parent: node.target,
                data: data
            });

        },
        error: function () {
            $.messager.alert('系统提示', "数据加载失败！", "error");
        }
    });
}

function treeEvent2()
{
    $(".easyui-tree2").tree({
        onClick: function(node){
            var level = node.level;
            var area_id = node.area_id;
            var area_name = node.text;
            if(level==4) {
                $("#area_id2").val(area_id);
                $("#area_name").val(area_name);
                /*社区可编辑*/
                var len = polygon.length;
                for(var i=0;i<len;i++) {
                    polygon[i].disableEditing();
                    if(area_id==polygon[i].area_id) {
                        polygon[i].enableEditing();
                    }
                }
                /*新增的社区设置为空*/
                map.removeOverlay(polygon_new);
                polygon_new = null;

            } else {
                $("#area_id2").val('');
                $("#area_name").val('');
                /*社区可编辑*/
                var len = polygon.length;
                for(var i=0;i<len;i++) {
                    polygon[i].disableEditing();
                }
                map.removeOverlay(polygon_new);
                polygon_new = null;
            }

        },
        onExpand: function(node){
            var area_id = node.area_id2;
            var level = node.level;
            if(level==3) {
                var childrenNodes = $('.easyui-tree2').tree('getChildren',node.target);
                var len = childrenNodes.length;
                for(var i=0;i<len;i++) {
                    $('.easyui-tree2').tree('remove',childrenNodes[i].target);
                }
                getChildArea2(node,area_id);
            }
        }
    });
}
function getChildArea2(node,area_id)
{
    $.ajax({
        type: "GET",
        url: "/admin/getChildPoints",
        data: {area_id:area_id},
        success: function (result) {
            var len = result.length;
            var data = [];
            for(var i=0;i<len;i++) {
                var tmp_obj = new Object();
                tmp_obj.text = "<a style='color: #000000' target='myFrameName' href='checktimescontent?level=4&city="+result[i].id+"'>"+result[i].name+"</a>";
                tmp_obj.state = 'open';
                tmp_obj.level = 4;
                tmp_obj.area_id = result[i].id;
                data.push(tmp_obj);
            }
            console.log(data);
            $('.easyui-tree2').tree('append', {
                parent: node.target,
                data: data
            });

        },
        error: function () {
            $.messager.alert('系统提示', "数据加载失败！", "error");
        }
    });
}